package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.views.StatementData;
import com.xmy.cultivate.service.IFinanceService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStudentGradeService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/api/finance")
public class FinanceController {

    @Autowired
    @Lazy
    IFinanceService iFinanceService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    IStudentGradeService iStudentGradeService;

    /**
     * 财务报表
     *
     * @param reMap
     * @return
     */
    @GetMapping("/statementData")
    public Result statementData(@RequestParam Map reMap) {

        Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
        Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));

        String schoolId = (String) reMap.get("schoolId");
        String startDate = (String) reMap.get("startDate");
        String endDate = (String) reMap.get("endDate");
        String yearPart = (String) reMap.get("yearPart");
        String quarterNum = (String) reMap.get("quarterNum");

        String subjectsId = (String) reMap.get("subjectsId");
        String studentName = (String) reMap.get("studentName");

        if (StringUtils.isBlank(yearPart)) {
            return new Result(Code.ERROR, "", "年份不能为空");
        }

        if (StringUtils.isBlank(quarterNum)) {
            return new Result(Code.ERROR, "", "学期不能为空");
        }

        IPage<StatementData> iPage = new Page<>(pageNum, pageSize);

        QueryWrapper<Quarter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part", yearPart);
        queryWrapper.eq("num", quarterNum);
        Quarter quarter = iQuarterService.getOne(queryWrapper);

        iFinanceService.statementData(iPage, schoolId, startDate, endDate, subjectsId, studentName, quarter, reMap);
        //排序 前台 处理 后台就先注示掉
//        if (reMap.get("studentSort") != null) {
//            int studentSort = Integer.valueOf(reMap.get("studentSort").toString());
//            List<StatementData> records = iPage.getRecords();
////            System.out.println("records前:"+records);
//            ArrayList<StatementData> objects = new ArrayList<>();
//            int size = records.size();
//            int isIn = 0;
//            for (int val = 0; val < size; val++) {
//                StatementData statementData = null;
//                if (records.size() == 1) {
//                    objects.add(records.get(0));
//                    if (size > 1) {
//                        isIn = 1;
//                    }
//                    break;
//                }
//                for (StatementData val2 : records) {
//                        if (statementData == null) {
//                            statementData = val2;
//                            continue;
//                        }
//                        if (studentSort == 7) {
//                            if (statementData.getUseCourseCount() < val2.getUseCourseCount()) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 8) {
//                            if (statementData.getArriveCourseCount() != null && val2.getArriveCourseCount() != null && statementData.getArriveCourseCount() < val2.getArriveCourseCount()) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 9) {
//                            if (statementData.getLeaveCourseCount() < val2.getLeaveCourseCount()) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 10) {
//                            if (statementData.getResidueCourseCount() < val2.getResidueCourseCount()) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 11) {
//                            if (Double.parseDouble(statementData.getResiduePrice().toString()) < Double.parseDouble(val2.getResiduePrice().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 12) {
//                            if (Double.parseDouble(statementData.getAbnormalPrice().toString()) < Double.parseDouble(val2.getAbnormalPrice().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 13) {
//                            if (Double.parseDouble(statementData.getPrice().toString()) < Double.parseDouble(val2.getPrice().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 14) {
//                            if (Double.parseDouble(statementData.getDiscountsPrice().toString()) < Double.parseDouble(val2.getDiscountsPrice().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 15) {
//                            if (Double.parseDouble(statementData.getSolidPrice().toString()) < Double.parseDouble(val2.getSolidPrice().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 16) {
//                            if (Double.parseDouble(statementData.getOutPrice().toString()) < Double.parseDouble(val2.getOutPrice().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 17) {
//                            if (Double.parseDouble(statementData.getRealityPirce().toString()) < Double.parseDouble(val2.getRealityPirce().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                        if (studentSort == 18) {
//                            if (Double.parseDouble(statementData.getAdvancePrice().toString()) < Double.parseDouble(val2.getAdvancePrice().toString())) {
//                                statementData = val2;
//                            }
//                            isIn = 1;
//                        }
//                }
//
//                if (statementData != null) {
//                    objects.add(statementData);
//                    records.remove(statementData);
//                }
//            }
////            System.out.println("records后:"+records);
//            if (isIn == 1) {
//                iPage.setRecords(objects);
//            }
////            System.out.println("objects:"+objects);
//        }
        //序号
        ArrayList<Object> objects = new ArrayList<>();
        int i = 1;
        for (StatementData val: iPage.getRecords()) {
            HashMap<Object, Object> rs = new HashMap<>();
            rs.put("id", val.getId());
            rs.put("studentId", val.getStudentId());
            rs.put("schoolId", val.getSchoolId());
            rs.put("studentName", val.getStudentName());
            rs.put("schoolName", val.getSchoolName());
            rs.put("yearClassId", val.getYearClassId());
            rs.put("yearClassName", val.getYearClassName());
            rs.put("subjectsId", val.getSubjectsId());
            rs.put("gradeId", val.getGradeId());
            rs.put("useCourseCount", val.getUseCourseCount());
            rs.put("arriveCourseCount", val.getArriveCourseCount());
            rs.put("leaveCourseCount", val.getLeaveCourseCount());
            rs.put("residueCourseCount", val.getResidueCourseCount());
            rs.put("residuePrice", val.getResiduePrice());
            rs.put("abnormalPrice", val.getAbnormalPrice());
            rs.put("price", val.getPrice());
            rs.put("discountsPrice", val.getDiscountsPrice());
            rs.put("solidPrice", val.getSolidPrice());
            rs.put("outPrice", val.getOutPrice());
            rs.put("realityPirce", val.getRealityPirce());
            rs.put("advancePrice", val.getAdvancePrice());
            rs.put("inReadStudentCount", val.getInReadStudentCount());
            rs.put("errorStudentCount", val.getErrorStudentCount());
            rs.put("stopStudentCount", val.getStopStudentCount());
            rs.put("returnStudentCount", val.getReturnStudentCount());
            rs.put("overStudentCount", val.getOverStudentCount());
            rs.put("changeStudentCount", val.getChangeStudentCount());
            rs.put("allCourseCount", val.getAllCourseCount());
            rs.put("index", i);
            i++;
            objects.add(rs);
        }
        return new Result(Code.OK, new PageResult(objects, iPage.getTotal(), iPage.getPages()));
    }

    @GetMapping("/studentCount")
    @ApiOperation("学员状态统计")
    public Result countStudent(@RequestParam Map reMap) {
        Map<String, String> map = new HashMap<>(6);

        //在读学员
        Integer inReadStudentCount = iStudentGradeService.inReadStudentCount(reMap);
        map.put("inReadStudentCount", inReadStudentCount.toString());

        //异常学员
        Integer errorStudentCount = iStudentGradeService.abnormalStudentCount(reMap);
        map.put("errorStudentCount", errorStudentCount.toString());

        //停课学员
        Integer stopStudentCount = iStudentGradeService.stopStudentCount(reMap);
        map.put("stopStudentCount", stopStudentCount.toString());

        //退费学员
        Integer returnStudentCount = iStudentGradeService.returnStudentCount(reMap);
        map.put("returnStudentCount", returnStudentCount.toString());

        //结课学员
        Integer overStudentCount = iStudentGradeService.overStudentCount(reMap);
        map.put("overStudentCount", overStudentCount.toString());

        //转校学员
        Integer changeStudentCount = iStudentGradeService.changeStudentCount(reMap);
        map.put("changeStudentCount", changeStudentCount.toString());

        return new Result(Code.GET_OK, map, "查询成功~");
    }
    /**
     * 获取当前季度
     * @return
     */
    @GetMapping("/getQuarterForYearPart")
    public Result getQuarterForYearPart(){
        Quarter quarter = iQuarterService.getNowQuarter();
        return new Result(Code.OK,quarter);
    }
}
